package com.xiweicheng.test.springboot.ws;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptorAdapter;

import lombok.extern.slf4j.Slf4j;

/** 
 * stomp连接处理类 
 * Created by earl on 2017/4/19. 
 */
//@Component
@Slf4j
public class PresenceChannelInterceptor extends ChannelInterceptorAdapter {

	@Autowired
	CacheManager cacheManager;

	@Override
	public void postSend(org.springframework.messaging.Message<?> message, MessageChannel channel, boolean sent) {
		StompHeaderAccessor sha = StompHeaderAccessor.wrap(message);
		// ignore non-STOMP messages like heartbeat messages  
		if (sha.getCommand() == null) {
			return;
		}
		//这里的sessionId和accountId对应HttpSessionIdHandshakeInterceptor拦截器的存放key  
		//		String sessionId = sha.getSessionAttributes().get(Constants.SESSIONID).toString();
		//		String accountId = sha.getSessionAttributes().get(Constants.SKEY_ACCOUNT_ID).toString();
		//判断客户端的连接状态  
		log.debug("cmd: " + sha.getCommand().name());
		switch (sha.getCommand()) {
		case CONNECT:
			//			connect(sessionId, accountId);
			break;
		case CONNECTED:
			break;
		case DISCONNECT:
			//			disconnect(sessionId, accountId, sha);
			break;
		default:
			break;
		}
	}

	//连接成功  
	private void connect(String sessionId, String accountId) {
		log.debug(" STOMP Connect [sessionId: " + sessionId + "]");
		//存放至ehcache  
		String cacheName = Constants.WEBSOCKET_ACCOUNT;
		//若在多个浏览器登录，直接覆盖保存  
		//		cacheManager.put(cacheName, cacheName + accountId, sessionId);
	}

	//断开连接  
	private void disconnect(String sessionId, String accountId, StompHeaderAccessor sha) {
		log.debug("STOMP Disconnect [sessionId: " + sessionId + "]");
		sha.getSessionAttributes().remove(Constants.SESSIONID);
		sha.getSessionAttributes().remove(Constants.SKEY_ACCOUNT_ID);
		//ehcache移除  
		String cacheName = Constants.WEBSOCKET_ACCOUNT;
		//		if (CacheManager.containsKey(cacheName, cacheName + accountId)) {
		//			CacheManager.remove(cacheName, cacheName + accountId);
		//		}

	}

}